package com.hack.qiaoqiao.serve;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

public class JsonStream extends AsyncTask<String,Void,String> {
	Context background;
	public JsonStream(Context context){
		this.background=context;
	}
	@Override
	protected String doInBackground(String... urls) {
		// TODO Auto-generated method stub
	    //HttpClient httpclient = new DefaultHttpClient();
        HttpHost httpHost = new HttpHost(urls[0], 5050);  
        HttpParams httpParams = new BasicHttpParams();  
        httpParams.setParameter(ConnRouteParams.DEFAULT_PROXY, httpHost);  
        HttpClient httpClient = new DefaultHttpClient(httpParams);  
		HttpPost request = new HttpPost(urls[0]);
		
		JSONObject param = new JSONObject(); 
		String token =null;
		try{
			param.put("phone", urls[1]);
			param.put("password", urls[2]);
			param.put("request_type", urls[3]);
			StringEntity se = new StringEntity(param.toString());  
			request.setEntity(se);
			HttpResponse httpResponse = httpClient.execute(request);  
			String retSrc = EntityUtils.toString(httpResponse.getEntity()); 
			JSONObject result = new JSONObject( retSrc);  
			token = (String) result.get("result"); 
		}catch (Exception e){
			e.printStackTrace();
		}
		return token;
	}
	protected void onPostExecute(String aResult){
		if ( aResult.equals("no")){
			Toast.makeText(background, "No Response ", Toast.LENGTH_SHORT).show();
		}
	}
}
